package com.longrise.android.byjk.plugins.tabfirst;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.course.allcourse.AllCoursebxyxActivity;
import com.longrise.android.byjk.plugins.course.batchbuy.BatchbuyActivity;
import com.longrise.android.byjk.plugins.course.batchbuy.batchbuyconfirm.BatchbuyConfirmActivity;
import com.longrise.android.byjk.plugins.course.coursedetail.CourseDetailActivity2;
import com.longrise.android.byjk.plugins.course.freecourse.FreeCourseActivity;
import com.longrise.android.byjk.plugins.course.mycourse.MyCourseActivity;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.education.assist.CourseHelper;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.education.assist.Flag;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.education.insurance.InsuranceActivity;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoConsts;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.android.byjk.plugins.hra.HraMainActivity;
import com.longrise.common.permission.PermissionDialog;
import com.longrise.common.permission.PermissionUtil;
import com.longrise.android.byjk.plugins.tabfirst.news.NewsDetail2Activity;
import com.longrise.android.byjk.plugins.tabfirst.qrcode.QRcodeActivity;
import com.longrise.android.byjk.plugins.tabfirst.revision.ConsultationActivity;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebActivity;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.UmengStatisticsUtil;

/**
 * Created by callxkj on 2017/2/23.
 * 负责首页跳转处理
 */
public class HomeViewJumpHelper {

    private static final String TAG = "HomeViewJumpHelper";

    /**
     * @param context    activity
     * @param uniqueid   中间自定义按钮的唯一id
     * @param linkedtype 跳转原生页面2001 公司内部webview2002 内部网页需要传身份证号,userid.
     *                   拼写方法如下 &cardno=xxxx&userid=xxxx
     *                   外部网页2003 外部网页不用传身份证号,userid
     * @param text       按钮下面的文字
     */
    public static void btJump(final Context context, String uniqueid, String linkedtype, String text, String linkedurl) {
        CustomerModuleJumpHelper.toJump(context, uniqueid, linkedtype, text, linkedurl, null, null);
    }

    /**
     * 新手专区 保宝头条的跳转
     *
     * @param context    activity
     * @param type       HomeMultipleItem.type1 HomeMultipleItem.type2
     * @param url        如果是webview 则这个就是连接地址
     * @param text       有可能会用来当做标题
     * @param linkedtype 跳转的type 同上面的方法参数
     */
    public static void customerModuleJump(Context context, int type, String url, String text, String linkedtype, String uniqueid) {
        if ("2003".equals(linkedtype)) {
            CustomerModuleJumpHelper.publicWebJump(context, text, url);
        } else {
            customerModuleJump2(context, type, url, text, linkedtype, uniqueid);
        }
    }

    private static void customerModuleJump2(Context context, int type, String url, String text, String linkedtype, String uniqueid) {
        switch (type) {
            case HomeMultipleItem.TYPE1:
                Bundle bundle = new Bundle();
                String userid = UserInfor.getInstance().getUserid();
                url = url + "&userid=" + userid;
                bundle.putString(PublicWebActivity.URL, url);
                bundle.putString(PublicWebActivity.BACK_TYPE, PublicWebActivity.BACK_ARROW);
                jump(context, PublicWebActivity.class, bundle);
                break;

            case HomeMultipleItem.TYPE2:
                if (TextUtils.isEmpty(url)) {
                    return;
                }
                Bundle bundle2 = new Bundle();
                bundle2.putString(NewsDetail2Activity.EXTRA_NEW_URL, url);
                jump(context, NewsDetail2Activity.class, bundle2);
                break;

            case HomeMultipleItem.TYPE2_1: //跳转到资讯列表页面 保易健康头条
                UmengStatisticsUtil.onEvent("news_more");
                jump(context, ConsultationActivity.class, null);
                break;

            case HomeMultipleItem.TYPE4: //保易的跳转
                CustomerModuleJumpHelper.toJump(context, uniqueid, linkedtype, text, null, null, null);
                break;

            case HomeMultipleItem.TYPE7_1: //免费试看的查看更多
                jump(context, FreeCourseActivity.class, null);
                UmengStatisticsUtil.onEvent("freecourse_more");
                break;

            case HomeMultipleItem.TYPE7_2:  //免费试看的 每个item点击 跳转到课程详情页
                Bundle bundle72 = new Bundle();
                //在这里 url代表 courseid   text代表 realprice
                bundle72.putString(CourseDetailActivity2.INTENT_EXTRA_PARAMS, url);
                bundle72.putString(CourseDetailActivity2.COURSE_PRICE, text);
                jump(context, CourseDetailActivity2.class, bundle72);
                break;

            case HomeMultipleItem.TYPE8_1: //宣传视频图片的点击
                checkNetWorkAndtoStudy(context, url, text);
                break;

            case HomeMultipleItem.TYPE9_1: //首页批量购买 查看更多
                jump(context, BatchbuyActivity.class, null);
                UmengStatisticsUtil.onEvent("buy_more");
                break;

            case HomeMultipleItem.TYPE9_2: //首页批量购买 立即购买 一键购买全部
                Bundle bundletype9 = new Bundle();
                bundletype9.putString(BatchbuyConfirmActivity.ORDERTYPE, BatchbuyConfirmActivity.BUY_ALL);
                jump(context, BatchbuyConfirmActivity.class, bundletype9);
                UmengStatisticsUtil.onEvent("buy_all");
                break;

            case HomeMultipleItem.TYPE10_1: //首页hra 1个优惠券的提示
                jump(context, HraMainActivity.class, null);
                break;
        }
    }

    /**
     * 跳转到二维码界面
     *
     * @param context activity
     */
    public static void QRcodeJump(final Context context) {
        PermissionUtil.getInstance().requestPermission((Activity) context, PermissionUtil.Permission.CAMERA, context.getString(R.string.camera_permission), context.getString(R.string.camera_permission_hint), new PermissionUtil.PermissionCallBack() {
            @Override
            public void onSuccess() {
                try {
                    Camera camera = Camera.open();
                    if (camera == null) {
                        new PermissionDialog.Builder((Activity) context, context.getString(R.string.camera_permission_hint)).setRequestCode(PermissionUtil.Permission.CAMERA.getRequestCode()).build().show();
                    } else {
                        camera.release();
                        jump(context, QRcodeActivity.class, null);
                    }
                } catch (Exception e) {
                    new PermissionDialog.Builder((Activity) context, context.getString(R.string.camera_permission_hint)).setRequestCode(PermissionUtil.Permission.CAMERA.getRequestCode()).build().show();
                }
            }

            @Override
            public void onDenied() {
            }
        });
    }


    /**
     * banner 的点击跳转
     * 跳转原生页面2001
     * 公司内部webview2002
     * 外部网页2003
     *
     * @param bean 跳转链接地址
     */
    public static void BannerJump(Context context, EntityBean bean) {
        String linkedtype = bean.getString("linkedtype");
        String linkedurl = bean.getString("linkedurl");
        String urltype = bean.getString("urltype", ""); //如果为null的话 switch会报空指针异常
        String title = bean.getString("title");
        String userid = UserInfor.getInstance().getUserid();
        if ("2003".equals(linkedtype)) {  //外部第三方网站 暂时不提供这个功能 外部网页2003
            CustomerModuleJumpHelper.publicWebJump(context, title, linkedurl);
        } else if ("2002".equals(linkedtype)) { //公司内部webview2002
            switch (urltype) {
                case "flhd": //费率活动
                    linkedurl = linkedurl + "&userid=" + userid;
                    Bundle bundle = new Bundle();
                    bundle.putString(PublicWebActivity.URL, linkedurl);
                    jump(context, PublicWebActivity.class, bundle);
                    break;
                case "wdxq": //问答详情
//                    Bundle wdBundle = new Bundle();
//                    wdBundle.putString(AnswerDetailActivity.EXTRA_ANSWER_URL, linkedurl);
//                    jump(context, AnswerDetailActivity.class, wdBundle);
                    break;
                case "zxxq": //资讯详情
                    Bundle zxBundle = new Bundle();
                    zxBundle.putString(NewsDetail2Activity.EXTRA_NEW_URL, linkedurl);
                    jump(context, NewsDetail2Activity.class, zxBundle);
                    break;
                default:

                    break;
            }
        } else if ("2001".equals(linkedtype)) {
            switch (urltype) {
                case "mfsk":  //免费课程
                    jump(context, FreeCourseActivity.class, null);
                    break;
                case "plgm":  //批量购买
                    jump(context, BatchbuyActivity.class, null);
                    break;
                case "hra":  //Hra预约页面
                    jump(context, HraMainActivity.class, null);
                    break;
                case "wdkc":  //我的课程
                    jump(context, MyCourseActivity.class, null);
                    break;
                case "grzy":  //电子名片
                    jump(context, PersonalShowActivity2.class, null);
                    break;
                case "bxyx":  //保险医学
                    jump(context, AllCoursebxyxActivity.class, null);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 签到的跳转
     */
    public static void signJump(Context context) {
        CustomerModuleJumpHelper.toJump(context, "1025", null, null, null, null, null);
    }


    public static void jump(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }


    /**
     * 检查是否是wifi网络
     */
    public static void checkNetWorkAndtoStudy(final Context context, final String url, final String text) {
        if (netWorkComformWIFI()) {
            toVideoPage(context, url, text, 1);
        } else {
            //移动网络环境下：弹窗提示 是否允许进入课程播放页
            CourseHelper.showNetWorkStateDialog(context, Flag.TRANSFER_COURSE_DETAIL, new Flag.OnNextListener() {
                @Override
                public void onBack(boolean isCloseCurrentPage) {
                    //不做任何处理
                }

                @Override
                public void toNext() {
                    toVideoPage(context, url, text, 2);
                }
            });
        }
    }

    /**
     * 首页的公司介绍视频
     */
    private static void toVideoPage(Context context, String url, String text, int flag) {
        VideoParams params = new VideoParams();
        params.mVideoPath = url;
        params.mCwidName = text;
        if (flag == 2) {  //如果是移动网络 用户点击了允许 就添加标记
            params.mDataNet = true;
        }
        Bundle bundle81 = new Bundle();
        bundle81.putParcelable(VideoConsts.VIDEO_PARAMS_PARCELABLE, params);
        bundle81.putInt(InsuranceActivity.VIDEO_KIND, 1);
        jump(context, InsuranceActivity.class, bundle81);
    }

    /**
     * 判断网络状态是否是WIFI（
     * 如果是移动网络环境下，判断用户是否已开启允许移动网络环境下学习视频
     * 否则只要不是移动网络环境下（包括没有网络）直接进入播放器
     */
    private static boolean netWorkComformWIFI() {
        //判断当前是否是移动网络环境
        boolean netWorkState = NetUtil.isDataNet();
        if (netWorkState) {
            //当前为移动网络环境，判断是否允许了移动网络环境下播放器视频课件,如果允许则返回true;
            String allow = Control.getCellularNetWork();
            return "1".equals(allow) ? true : false;
        }
        //否则直接返回true
        return true;
    }
}
